In Express if you have two route paths with same address, rather than throwing an error, it will pick theroute path that was defined first in App.js.
Routes are defined using an HTTP verb and a path pattern. Any request to the server that matches a route definition is routed to the associated route handler. Routes are URL schema for a website. Express routes can be strings or regular expressions. Express, by default, supports the following HTTP request methods
UNSUBSCRIBE Not supported:
LINK
You can create routes with other HTTP verbs . app.get('/',function(req,res) { res.send("I am Foo"); });
app.put('/',function(req,res) { res.send(req.body); });
app.post('/',function(req,res) { res.send(req.body); });
app.delete('/',function(req,res) { res.send(req.body); });
you may want to create only one route path where the id parameter is optional app.get('/:id?',function(req,res) { if(req.params.id) { res.send("I am Foo with id " + req.params.id); } else { res.send("I am Foo"); } }); This route can handle both types of requests, either with a parameter or without one.
Express allow you to use a wildcard within a route path using app.get('/',function(req,res) { req.send("I am Foo"); });
Pros Routes can be defined as regular expressions It is not a straightforward approach helps to create flexible and powerful route patterns. Cons: hard to debug and maintain.
Do not quote the regular expression object, or else you will get unexpected results.
Input parameters are parsed and accessible via req.params. req.body object is used to handle data in HTTP POST request. There are two types of parametric GET requests in Express.js - req.params and req.query.
req.params will return parameters in the matched route. If your route is /user/:id and you make a request to /user/2 - req.params would yield {id: "2"} To handle request parameters in URLs like http://example.com/product/1274, You can define a route as app.get('/user/:id', function(req, res){ res.send("User Id : "+ req.params.id) }); It uses the clean URL scheme
The req.query object contains request parameters in the GET query. req.query will return a JS object after the query string is parsed. It uses the conventional GET request scheme. Eg: http://example.com?name=john&age=24. In this case req.query would yield {name:"tom", age: "55"}
This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.
// POST user[name]=tobi&user[email]=tobi@learnboost.com req.body.user.name // => "tobi"
req.body.user.email // => "tobi@learnboost.com"
// POST { "name": "tobi" } req.body.name // => "tobi"
Return the value of param name when present.
// ?name=tobi req.param('name') // => "tobi"
// POST name=tobi req.param('name') // => "tobi"
// /user/tobi for /user/:name req.param('name') // => "tobi" Lookup is performed in the following order:
req.params req.body req.query Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.
Returns the request URL pathname.
// example.com/users?sort=desc req.path // => "/users"
HTTP verbs- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html Organize routes in Node.js- https://github.com/visionmedia/express/tree/master/examples Understanding Express Routes- http://www.packtpub.com/article/understanding-express-routes expressjs middleware - http://expressjs.com/guide.html#middleware